package com.excilys.formation.project.controllers.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import com.excilys.formation.project.entities.Client;
import com.excilys.formation.project.model.ClientUser;
import com.excilys.formation.project.services.CompteService;

/**
 * 
 * @author excilys
 * 
 */
@Component
public final class AuthentifiedClientManager {
    /**
     * Service des comptes pour récupérer les comptes du client.
     */
    @Autowired
    private CompteService compteService;

    /**
     * 
     * @return le client de la session
     */
    public Client getSessionClient() {
        Client connectedClient = ClientUser.class.cast(SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getClient();
        connectedClient.setComptes(compteService.getComptesByClient(connectedClient.getId()));
        return connectedClient;
    }
}
